package com.autonavi.yunda.yunji.core.model.dto;

import com.autonavi.yunda.yunji.common.utils.JsonUtils;
import com.autonavi.yunda.yunji.core.enums.ComponentSourceType;
import com.autonavi.yunda.yunji.core.enums.ComponentType;
import com.autonavi.yunda.yunji.core.enums.PartUnitInterfaceStatus;
import com.autonavi.yunda.yunji.core.model.vo.ComponentData;
import com.autonavi.yunda.yunji.core.model.vo.PartUnitExtraData;
import com.autonavi.yunda.yunji.core.service.thirdpartyservice.vo.TagVO;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "yunji_raw_part_unit", autoResultMap = true)
public class PartUnitDTO {
    @TableId(type = IdType.AUTO)
    private Long id;
    private ComponentType type;
    private String version;
    private String code;
    private PartUnitInterfaceStatus status;
    private String title;
    @TableField("falcon_id")
    private String falconId;
    @TableField("yuncang_id")
    private String yuncangId;
    @TableField(value = "tags", typeHandler = JacksonTypeHandler.class)
    private List<TagVO> tags;
    @TableField(value = "unit_list", typeHandler = JacksonTypeHandler.class)
    private List<ComponentData> unitList;
    @TableField(value = "part_list", typeHandler = JacksonTypeHandler.class)
    private List<ComponentData> partList;
    /**
     * 零部件来源(编排/导入)
     */
    @TableField("source_type")
    private ComponentSourceType sourceType;
    @TableField("source_id")
    private Long sourceId;
    @TableField(value = "extra_data", typeHandler = JacksonTypeHandler.class)
    private PartUnitExtraData extraData;
    @TableField("create_user")
    private String createUser;
    @TableField("update_user")
    private String updateUser;
    @TableField("create_time")
    private Timestamp createTime;
    @TableField("update_time")
    private Timestamp updateTime;
    private Long bdId;

    public PartUnitExtraData getExtraData() {
        if (extraData == null) {
            return new PartUnitExtraData();
        }
        return extraData;
    }

    public List<TagVO> getTags() {
        if (tags == null) {
            return new ArrayList<>();
        }
        return JsonUtils.from(JsonUtils.toString(tags), new TypeReference<>() {
        });
    }

    public List<ComponentData> getUnitList() {
        List<ComponentData> unitVOS = JsonUtils.from(JsonUtils.toString(unitList), new TypeReference<>() {
        });
        if (unitVOS == null) {
            unitVOS = new ArrayList<>();
        }
        this.unitList = unitVOS;
        return unitList;
    }

    public List<ComponentData> getPartList() {
        List<ComponentData> partVOS = JsonUtils.from(JsonUtils.toString(partList), new TypeReference<>() {
        });
        if (partVOS == null) {
            partVOS = new ArrayList<>();
        }
        this.partList = partVOS;
        return partList;
    }
}
